<?php

namespace app\ow_test\controller;


use think\Controller;
use think\Db;
use think\Loader;
use think\response\Json;
use function foo\func;
use think\facade\Env;

//require_once Env::get('ROOT_PATH')."extend/snoopyclasscoder2/Snoopy.class.php";
require_once Env::get('ROOT_PATH') . "extend/Snoopy-2.0.0.tar.gz/Snoopy.class.php";


header("Content-type:text/html;charset=UTF-8");

class Bilibili extends \think\Controller
{

    public function bilibili()
    {
        $header = [
            "content-type: text/plain; charset=utf-8",
            "accept-language: zh-CN,zh;q=0.9",
            "accept-encoding: gzip, deflate, br",
            "user-agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36"
        ];



     //   $url = "https://api.bilibili.com/x/v2/reply/main";
        //$url = 'http://c.runoob.com/front-end/53';
        $url = 'https://www.bilibili.com/video/BV1vE411x7oY?from=search&seid=10289515081333516437';
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_ENCODING, 'gzip');

        //规避SSL验证
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        $data = curl_exec($curl);
        $data = mb_convert_encoding($data, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');
        curl_close($curl);
        // echo htmlspecialchars($data);
        echo $data;

        // htmlspecialchars();


    }


    //通过fopen的方式来爬取网页
    public function getFopen()
    {

        //请注意目前来说打不开https的网站
        $url = 'http://c.runoob.com/front-end/53';
        //$url = 'https://www.bilibili.com/video/BV1vE411x7oY?from=search&seid=10289515081333516437';

        $file = fopen($url, 'r');
        $content = "";

        if ($file) {

            while (($contentTest = fgets($file)) !== false) {
                $content = $content . $contentTest;

            }
            fclose($file);

            echo $content;

        } else {

            echo "爬取失败";

        }
    }

    //用file_get_contents来爬取网页
    public function getFileGetContents()
    {


        $url = 'http://c.runoob.com/front-end/53';
        // $url = 'https://www.bilibili.com/video/BV1vE411x7oY?from=search&seid=10289515081333516437';
        $content = file_get_contents($url);
        echo $content;
    }

    public function getSnoopy()
    {

        $url = 'http://c.runoob.com/front-end/53';
        //$url = 'https://www.bilibili.com/video/BV1vE411x7oY?from=search&seid=10289515081333516437';

        //使用第三方类库
        $snoopy = new \snoopy();
        //fetch 得到整个网页
        $snoopy->fetch($url);
        echo $snoopy->results;

    }


}